<!DOCTYPE html>

<html lang='en'>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/react.development16.4.0.js"></script>
    <script src="./lib/react-dom.development16.4.0.js"></script>
    <script src="./lib/babel.min.js"></script>
</head>

<body>
    <div>
     <h1>列表添加操作</h1>
     <input type="text"/><input type="button" value="ADD"/>
     <ul>
         <li>吃饭</li>
         <li>睡觉</li>
         <li>打豆豆</li>
         <li>学习</li>
     </ul>
    </div>
    <hr/>
    <div id="app">
    </div>
    <!-- 
    分析问题：
     我们可以将页面分成3个组件：
     父组件 APP
     子组件2个： AddDo
     ListCom
     -->
    <script type="text/babel">
     
        class App extends React.Component {
            constructor(props) {
                super(props);
                this.state={
                    list:['吃饭','睡觉','打豆豆','学习']
                }
                this.addList = this.addList.bind(this);
            }
           
           addList(newVal){
               let list1 = this.state.list;
               list1.push(newVal);
               this.setState({
                   list:list1
               })
           }
            render() {
                return (
                    <div>
                        <h1>列表添加操作</h1>
                        <AddDo addList={this.addList} length={this.state.list.length}/>
                        <ListCom list={this.state.list} />
                    </div>
                )
            }
        }
        
        class AddDo extends  React.Component {
            
            addDo(){
              let val = this.refs.myinput.value;
              //验证非空
              if(!val.trim())
              {
                  return
              }
              this.props.addList(val);
              this.refs.myinput.value='';
            }
            render() {
                return (
                    <div>
                        <input ref="myinput" type="text"/>
                        <button onClick={()=>this.addDo()}>ADD---{this.props.length}</button>
                    </div>
                )
            }
        }
        
        class ListCom extends  React.Component {
            render() {
                let {list} = this.props;
                return (
                    <div>
                        <ul>
                          {
                             list.map((item,index)=><li key={index}>{item}</li>) 
                          }
                        </ul>
                    </div>
                )
            }
        }

        ReactDOM.render(
            <App />,document.getElementById('app')
        );
    </script>
</body>

</html>